using SmartCommunity.Models;
using Mapster;
using System.ComponentModel.DataAnnotations;

namespace SmartCommunity.WebApi.Services.System
{
    #region DTO属性映射
    /// <summary>
    /// 属性映射 UserDtoRegister
    /// </summary>
    public class UserDtoRegister : IRegister
    {
        public void Register(TypeAdapterConfig config)
        {
            config.ForType<UserEntity, UserDto>()
            .Map(d => d.RoleIds, s => s.Roles.Select(a => a.RoleId), s => s.Roles != null)
            ;
        }
    }
    #endregion

    #region 数据传输对象 UserDto
    /// <summary>
    /// 数据传输对象：用户
    /// </summary>
    public class UserDto : BaseDto
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserId { get; set; }

        /// <summary>
        /// 登录用户名
        /// </summary>
        [Required(ErrorMessage = "登录用户名不能为空")]
        [StringLength(50, ErrorMessage = "登录用户名 长度不能超过 50.")]
        public string Username { get; set; } = string.Empty;

        /// <summary>
        /// 昵称
        /// </summary>
        [Required(ErrorMessage = "昵称不能为空")]
        [StringLength(50, ErrorMessage = "昵称 长度不能超过 50.")]
        public string Nickname { get; set; } = string.Empty;


        /// <summary>
        /// 手机号码
        /// </summary>
        [Required(ErrorMessage = "手机号码不能为空")]
        [StringLength(20, ErrorMessage = "手机号码 长度不能超过 20.")]
        public string Cellphone { get; set; } = string.Empty;

        /// <summary>
        /// 电子邮箱
        /// </summary>
        [StringLength(80, ErrorMessage = "电子邮箱 长度不能超过 80.")]
        public string? Email { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        public string? Gender { get; set; }

        public string GenderName { get { return Gender == "M" ? "男" : Gender == "F" ? "女" : "保密"; } }

        /// <summary>
        /// 是否作废
        /// </summary>
        public bool Disabled { get; set; }

        /// <summary>
        /// 用户状态:0正常；1密码锁定；2密码过期；
        /// </summary>
        public short Status { get; set; }

        public string StatusName
        {
            get
            {
                return Disabled ? "作废" : Status == 0 ? "正常" : Status == 1 ? "密码锁定" : Status == 2 ? "密码过期" : "状态异常";
            }
        }

        /// <summary>
        /// 头像
        /// </summary>
        [StringLength(200, ErrorMessage = "头像 长度不能超过 200.")]
        public string? Avatar { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [StringLength(255, ErrorMessage = "备注 长度不能超过 255.")]
        public string? Remark { get; set; }

        /// <summary>
        /// 角色ID集合
        /// </summary>
        public IEnumerable<int> RoleIds { get; set; } = new List<int>();

    }
    #endregion

    #region 用户中心DTO
    public class UserDataDto
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserId { get; set; }

        /// <summary>
        /// 昵称
        /// </summary>
        [Required(ErrorMessage = "昵称不能为空")]
        [StringLength(50, ErrorMessage = "昵称 长度不能超过 50.")]
        public string Nickname { get; set; } = null!;

        /// <summary>
        /// 手机号码
        /// </summary>
        [Required(ErrorMessage = "手机号码不能为空")]
        [StringLength(20, ErrorMessage = "手机号码 长度不能超过 20.")]
        public string Cellphone { get; set; } = null!;

        /// <summary>
        /// 电子邮箱
        /// </summary>
        [StringLength(80, ErrorMessage = "电子邮箱 长度不能超过 80.")]
        public string? Email { get; set; } = null!;

        /// <summary>
        /// 头像
        /// </summary>
        [StringLength(200, ErrorMessage = "头像 长度不能超过 200.")]
        public string? Avatar { get; set; }

        /// <summary>
        /// 自我介绍
        /// </summary>
        [StringLength(255, ErrorMessage = "自我介绍 长度不能超过 255.")]
        public string? Introduction { get; set; }
    }
    #endregion

    #region 数据查询对象
    /// <summary>
    /// 查询模型：用户
    /// </summary>
    public class UserQuery : AbstractQuery<UserEntity>
    {
        /// <summary>
        /// 主键ID多选查询
        /// </summary>
        public IEnumerable<int> Ids { get; set; } = [];

        public bool? Disabled { get; set; }

        /// <summary>
        /// 根据查询条件添加查询表达式
        /// </summary>
        protected override void MergeQueryExpression()
        {
            AddExpression(Ids.Any(), e => Ids.Contains(e.UserId));
            AddExpression(Disabled.HasValue, r => r.Disabled == Disabled!.Value);
            AddSearchExpression(s => o => o.Username.Contains(s) || o.Nickname.Contains(s) || o.Cellphone.Contains(s) || (o.Email ?? "").Contains(s));
            AddDateExpression(start => e => e.CreatedTime >= start, end => e => e.CreatedTime <= end);
        }
    }
    #endregion
}
